package view.shop
{
import core.manager.ShopManager;
import core.model.PlayerModel;
import core.model.events.PlayerModelEvent;

import flash.display.MovieClip;
import flash.geom.Point;

import framework.view.components.container.IRenderProxy;

import values.shop.ShopConfigValue;
import values.shop.ShopGroupTypeValue;
import values.shop.ShopGroupValue;

import view.component.window.MultipleClassifiedPaginationWindowProxy;
import view.component.window.event.NavigationChangeEvent;

/**
 * ShopUIProxy
 * @author jliang
 * 
 */
public class ShopUIProxy extends MultipleClassifiedPaginationWindowProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function ShopUIProxy()
	{
		super();
	}
    //==========================================================================
    //  Properties
    //==========================================================================
    [Inject]
    public var playerModel:PlayerModel;
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override protected function get repeaterItemRenderClass():Class
	{
		return ShopItemRenderProxy;
	}
	
	override protected function get navItemRenderClass():Class
	{
		return ShopNavRenderProxy;
	}
    
    override protected function get subNnavItemRenderClass():Class
    {
        return ShopSubNavRenderProxy;
    }
    
    override protected function get navData():*
    {
        return ShopConfigValue.getShopGroupList();
    }
    
    override protected function get currentSubTypeData():*		
    {
        return ShopGroupTypeValue(currentTypeData[int(currentSubType) - 1]).itemList;
    }
    
    override protected function get currentSubTypeDataLength():*		
    {
        return currentSubTypeData.length;
    }
	
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
	}
	
	override protected function initData():void
	{
		for each (var vo:ShopGroupValue in ShopConfigValue.getShopGroupList())
		{
			addTypeData(String(vo.index), vo.typeList);
		}
	}
	//==========================================================================
	//  Public methods
	//==========================================================================    
    override public function show(lock:Boolean=true, position:Point=null, ...args):void
    {
        super.show.apply(this, [ lock, position ].concat(args));
        
        playerModel.addEventListener(PlayerModelEvent.ITEM_NUM_CHANGED, resourceChangedHandler);
        
        ShopManager.syncUpLimit();
    }
    
    override public function close():void
    {
        super.close();
        
        playerModel.removeEventListener(PlayerModelEvent.ITEM_NUM_CHANGED, resourceChangedHandler);
    }
    
    public function showShop(index:int = -1, itemKey:String = null):void
	{
        show();
        
        if (index == -1 && currentType != null)
        {
            refresh();
            return;
        }
        else if (index == -1 && currentType == null)
        {
            index = 1;
        }
        
		for each (var render:IRenderProxy in navRepeater.itemProxys)
		{
			if (render.data && index > 0 && ShopGroupValue(render.data).index == index || !index == -1)
			{
				render.selected = true;
				break;
			}
		}
	}
    
    public function refresh():void
    {
        var pageCount:int = Math.ceil(currentSubTypeDataLength / preCount);
        
        var page:int = currentPage;
        if (pageCount - 1 < currentPage)
        {
            page = pageCount - 1;
            if (page < 0)
            {
                page = 0;
            }
        }
        updateList(currentSubTypeData, currentSubTypeDataLength, page);
    }
	//==========================================================================
	//  Event handlers
	//==========================================================================	
	override protected function navigationChangeHandler(event:NavigationChangeEvent):void
	{
        var vo:ShopGroupValue = ShopGroupValue(event.param);
        
        for each (var render:IRenderProxy in navRepeater.itemProxys)
        {
            if (render.data && ShopGroupValue(render.data).index != vo.index)
            {
                render.selected = false;
            }
        }
        currentType = String(vo.index);
        
        subNavRepeater.data = currentTypeData;
        subNavRepeater.itemProxys[0].selected = true;
	}
    
    override protected function subNavigationChangeHandler(event:NavigationChangeEvent):void
    {
        var vo:ShopGroupTypeValue = ShopGroupTypeValue(event.param);
        
        for each (var render:IRenderProxy in subNavRepeater.itemProxys)
        {
            if (render.data && ShopGroupTypeValue(render.data).index != vo.index)
            {
                render.selected = false;
            }
        }
        currentSubType = String(vo.index);
        
        updateList(currentSubTypeData, currentSubTypeDataLength, 0);
    }
    
    protected function resourceChangedHandler(event:PlayerModelEvent):void
    {
        if (event.params.isResource != true) return;
        
        for each (var render:IRenderProxy in itemRepeater.itemProxys)
        {
            if (render.data != null)
            {
                ShopItemRenderProxy(render).checkBuyable();
            }
        }
    }
}
}


